public class B2 {
    public static void main(String[] args) {
        Station station1 = new Station("窗口一");
        Station station2 = new Station("窗口二");
        Station station3 = new Station("窗口三");
        station1.start();
        station2.start();
        station3.start();

    }
}

class Station extends Thread {
    static int ticket = 50;
    static Object ob = "ThisIsATicket";
    public Station(String name) {
        super(name);
    }
    public void run() {
        sell();
    }
    public void sell() {
        while (ticket > 0) {
            synchronized (ob) {
                if (ticket > 0) {
                    if (getName().equals("窗口三") && Math.random() > 0.5 && ticket < 50) {
                        ticket++;
                        System.out.println("退票第" + ticket + "张票");
                    } else {
                        System.out.println(getName() + "卖了第" + ticket + "张票");
                        ticket--;
                    }

                } else {
                    System.out.println("很抱歉，票卖完了");
                }
            }

            try {

                sleep((long) (1000 * Math.random()));
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}